package com.yonyou.pmclouds.pmpub;


import com.alibaba.dubbo.config.annotation.Reference;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.entity.AdditionalPropertiesVOWrapper;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.exception.IllegalRequestArgException;
import com.yonyou.pmclouds.basecom.util.BeanHelper;
import com.yonyou.pmclouds.basecom.util.CommonFieldConst;
import com.yonyou.pmclouds.basecom.util.OssUtil;
import com.yonyou.pmclouds.basecom.util.StorageFactory;
import com.yonyou.pmclouds.outter.enterprise.EnterpriseInfoQuery;
import com.yonyou.pmclouds.outter.user.UserInfoQuery;
import com.yonyou.pmclouds.pmpub.entity.PMListQueryResult;
import com.yonyou.pmclouds.pmpub.entity.QueryListParam;
import com.yonyou.pmclouds.pmpub.rmiitf.ICommonQueryService;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping({"/common/query","/mobile/common/query"})
public class CommonQueryController {

    @Reference(version = ApplicationConsts.APPLICATION_VERSION)
    ICommonQueryService queryService;
    @Autowired
    private StorageFactory storageFactory;
    @Autowired
    private UserInfoQuery userQuery;
    @Autowired
    private EnterpriseInfoQuery entQuery;

    private static final String[] USER_ID_FIELDS = {CommonFieldConst.CREATOR,CommonFieldConst.CREATOR};
    private static final String[] USER_NAME_FIELDS = {CommonFieldConst.CREATOR_NAME,"creator_name"};
    private static final String[] ENT_ID_FIELDS = {CommonFieldConst.PK_ENTERPRISE,"pkConstructionunit","pkConstructionunit"};
    private static final String[] ENT_NAME_FIELDS = {CommonFieldConst.ENTERPRISE_NAME,"constructionunit_name","constructionName"};



    @RequestMapping(value = "/queryByProject")
    @ResponseBody
    public PMListQueryResult queryListByPrj(@RequestBody @Validated QueryListParam queryListParam,
                                            BindingResult errors, HttpServletRequest request) throws BusinessException {
        if (errors.hasErrors()) {
            throw new IllegalRequestArgException();
        }

        PMListQueryResult result = queryService.queryListByPrj(queryListParam);

        dealResult(result, request);

        return result;
    }

    private void dealResult(PMListQueryResult result, HttpServletRequest request) {
        dealUserInfo(result);

        dealEntInfo(result);

        dealPicInfo(result, request);

        dealExtraInfo(result);
    }

    private void dealUserInfo(PMListQueryResult result) {
        if(ArrayUtils.isNotEmpty(result.getBusiVOs())){
            userQuery.tranUserInfoField(result.getBuziSuperVOs(), USER_ID_FIELDS, USER_NAME_FIELDS);
        }
    }

    private void dealEntInfo(PMListQueryResult result) {
        if(ArrayUtils.isEmpty(result.getBusiVOs()))
            return ;
        entQuery.tranEntInfoField(result.getBuziSuperVOs(),  ENT_ID_FIELDS, ENT_NAME_FIELDS);
    }

    private void dealPicInfo(PMListQueryResult result, HttpServletRequest request) {
        AdditionalPropertiesVOWrapper[] superVOs = result.getBusiVOs();
        if (ArrayUtils.isNotEmpty(superVOs)) {
            for (AdditionalPropertiesVOWrapper vo : superVOs) {
                Object picObj = BeanHelper.getProperty(vo.getWrapped(), CommonFieldConst.PICTURES);
                String[] pics = new String[]{};
                if(picObj instanceof List){
                    pics = ((List<String>)picObj).toArray(new String[0]);
                }else if(picObj instanceof String[]){
                    pics = (String[])picObj;
                }

                if (ArrayUtils.isEmpty(pics))
                    continue;
                vo.addProperties(CommonFieldConst.PICINFOS,storageFactory.getStorageUtil().signForAppPics(pics, request));
            }
        }
    }


    private PMListQueryResult dealExtraInfo(PMListQueryResult result){
        return result;
    }
}